using System.CodeDom.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.CodeDom;
using NBusiness.ESharp.Compiler;
using NBusiness.CodeDom.Compiler;
using System.IO;
using NBusiness.ESharp.Testing.Properties;
using NBusiness.ESharp.Testing.Mocks;
using NBusiness.CodeDom.Services;

namespace NBusiness.ESharp.Testing
{

    [TestClass]
    public class ActionTests
    {
        [TestMethod]
        public void ActionTest1()
        {
            ESharpProvider provider = new ESharpProvider();            
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Action.es", Resources.Action ));

            Assert.AreEqual(0, errorService.Errors.Count);

            Assert.AreEqual(1, root.Families.Count);
            Assert.AreEqual(1, root.Families["Test"].Entities.Count);

            Assert.AreEqual(22, root.Families["Test"].Entities[0].Actions.Count);

            Assert.AreEqual("Custom1", root.Families["Test"].Entities[0].Actions[0].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[0].On);
            Assert.AreEqual(EntityActionType.Never, root.Families["Test"].Entities[0].Actions[0].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[0].Async);

            Assert.AreEqual("Custom2", root.Families["Test"].Entities[0].Actions[1].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[1].On);
            Assert.AreEqual(EntityActionType.Loading, root.Families["Test"].Entities[0].Actions[1].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[1].Async);

            Assert.AreEqual("Custom3", root.Families["Test"].Entities[0].Actions[2].Name);
            Assert.AreEqual("Test.CustomActions", root.Families["Test"].Entities[0].Actions[2].On);
            Assert.AreEqual(EntityActionType.Never, root.Families["Test"].Entities[0].Actions[2].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[2].Async);

            Assert.AreEqual("Custom4", root.Families["Test"].Entities[0].Actions[3].Name);
            Assert.AreEqual("Test.CustomActions", root.Families["Test"].Entities[0].Actions[3].On);
            Assert.AreEqual(EntityActionType.Loaded, root.Families["Test"].Entities[0].Actions[3].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[3].Async);

            Assert.AreEqual("Custom5", root.Families["Test"].Entities[0].Actions[4].Name);
            Assert.AreEqual("Test.CustomActions", root.Families["Test"].Entities[0].Actions[4].On);
            Assert.AreEqual(EntityActionType.Fetching, root.Families["Test"].Entities[0].Actions[4].When);
            Assert.AreEqual(true, root.Families["Test"].Entities[0].Actions[4].Async);

            Assert.AreEqual("Custom6", root.Families["Test"].Entities[0].Actions[5].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[5].On);
            Assert.AreEqual(EntityActionType.Fetched, root.Families["Test"].Entities[0].Actions[5].When);
            Assert.AreEqual(true, root.Families["Test"].Entities[0].Actions[5].Async);

            Assert.AreEqual("Custom7", root.Families["Test"].Entities[0].Actions[6].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[6].On);
            Assert.AreEqual(EntityActionType.Persisting, root.Families["Test"].Entities[0].Actions[6].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[6].Async);

            Assert.AreEqual("Custom8", root.Families["Test"].Entities[0].Actions[7].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[7].On);
            Assert.AreEqual(EntityActionType.Persisted, root.Families["Test"].Entities[0].Actions[7].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[7].Async);

            Assert.AreEqual("Custom9", root.Families["Test"].Entities[0].Actions[8].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[8].On);
            Assert.AreEqual(EntityActionType.Inserting, root.Families["Test"].Entities[0].Actions[8].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[8].Async);

            Assert.AreEqual("Custom10", root.Families["Test"].Entities[0].Actions[9].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[9].On);
            Assert.AreEqual(EntityActionType.Inserted, root.Families["Test"].Entities[0].Actions[9].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[9].Async);

            Assert.AreEqual("Custom11", root.Families["Test"].Entities[0].Actions[10].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[10].On);
            Assert.AreEqual(EntityActionType.Updating, root.Families["Test"].Entities[0].Actions[10].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[10].Async);

            Assert.AreEqual("Custom12", root.Families["Test"].Entities[0].Actions[11].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[11].On);
            Assert.AreEqual(EntityActionType.Updated, root.Families["Test"].Entities[0].Actions[11].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[11].Async);

            Assert.AreEqual("Custom13", root.Families["Test"].Entities[0].Actions[12].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[12].On);
            Assert.AreEqual(EntityActionType.Deleting, root.Families["Test"].Entities[0].Actions[12].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[12].Async);

            Assert.AreEqual("Custom14", root.Families["Test"].Entities[0].Actions[13].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[13].On);
            Assert.AreEqual(EntityActionType.Deleted, root.Families["Test"].Entities[0].Actions[13].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[13].Async);

            Assert.AreEqual("Custom15", root.Families["Test"].Entities[0].Actions[14].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[14].On);
            Assert.AreEqual(EntityActionType.Validating, root.Families["Test"].Entities[0].Actions[14].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[14].Async);

            Assert.AreEqual("Custom16", root.Families["Test"].Entities[0].Actions[15].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[15].On);
            Assert.AreEqual(EntityActionType.Validated, root.Families["Test"].Entities[0].Actions[15].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[15].Async);

            Assert.AreEqual("Custom17", root.Families["Test"].Entities[0].Actions[16].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[16].On);
            Assert.AreEqual(EntityActionType.Authorizing, root.Families["Test"].Entities[0].Actions[16].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[16].Async);

            Assert.AreEqual("Custom18", root.Families["Test"].Entities[0].Actions[17].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[17].On);
            Assert.AreEqual(EntityActionType.Authorized, root.Families["Test"].Entities[0].Actions[17].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[17].Async);

            Assert.AreEqual("Custom19", root.Families["Test"].Entities[0].Actions[18].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[18].On);
            Assert.AreEqual(EntityActionType.Accessing, root.Families["Test"].Entities[0].Actions[18].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[18].Async);

            Assert.AreEqual("Custom20", root.Families["Test"].Entities[0].Actions[19].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[19].On);
            Assert.AreEqual(EntityActionType.Accessed, root.Families["Test"].Entities[0].Actions[19].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[19].Async);

            Assert.AreEqual("Custom21", root.Families["Test"].Entities[0].Actions[20].Name);
            Assert.AreEqual(null, root.Families["Test"].Entities[0].Actions[20].On);
            Assert.AreEqual(EntityActionType.PropertyChanged, root.Families["Test"].Entities[0].Actions[20].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[20].Async);

            Assert.AreEqual("Custom22", root.Families["Test"].Entities[0].Actions[21].Name);
            Assert.AreEqual("NBusiness.Test.MockAction", root.Families["Test"].Entities[0].Actions[21].On);
            Assert.AreEqual(EntityActionType.Fetching, root.Families["Test"].Entities[0].Actions[21].When);
            Assert.AreEqual(false, root.Families["Test"].Entities[0].Actions[21].Async);
        }
    }
}
